package com.share51.admin.shiro;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.mgt.SecurityManager;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;
import org.apache.shiro.util.ByteSource;

import javax.annotation.Resource;
import java.util.HashSet;
import java.util.Set;

public class MyRealm extends AuthorizingRealm {

    private String salt;

    public MyRealm(String salt) {
        this.salt = salt;
    }

   /* @Resource
    private UserService userService;

    @Resource
    private RoleService roleService;*/

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        String userName = (String) principals.getPrimaryPrincipal();
       /* User user = userService.findUserByUserName(userName);
        Role role = roleService.getRoleById(user.getRole());

        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        Set<String> roleSet=new HashSet<>();
        roleSet.add(role.getRoleName());
        info.setRoles(roleSet);*/
        return null;
    }

    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        String userName = (String) token.getCredentials();
       /* User user = userService.findUserByUserName(userName);
        if (user == null || user.getStatus() == UserConstant.UserState.DELETE) {

        }
        if (user.getStatus() == UserConstant.UserState.LOCKED) {

        }
        AuthenticationInfo info = new SimpleAuthenticationInfo(userName,
                user.getPassword(),
                ByteSource.Util.bytes(salt),
                this.getName());
        Subject subject = SecurityUtils.getSubject();
        subject.getSession().setAttribute(UserConstant.CURENT_USER, user);*/
        return null;
    }
}
